import Vue from 'vue'
import Vuex from 'vuex'
import {robotSnapShoot} from '../api/api'
import {timestampToTime} from '../common/js/util'

Vue.use(Vuex)

const state = {
  snapShootLine1: [],
  snapShootLine2: [],
  snapShootTotal: [],
  shootTotal: {
    baseAssetAvailableTotal: 0,
    baseAssetFreezeTotal: 0,
    quoteAssetAvailableTotal: 0,
    quoteAssetFreezeTotal: 0
  }
}

const getters = {
  getSnapShoot (_state) {
    return _state.snapShootLine
  }
}

const mutations = {
  setSnapShoot1 (_state, data) {
    _state.snapShootLine1 = data
  },
  setSnapShoot2 (_state, data) {
    _state.snapShootLine2 = data
  },
  setShootTotal (_state, data) {
    _state.snapShootTotal = data
  }
}

const actions = {
  acSnapShoot (_mutations, param) {
    robotSnapShoot(param).then((res) => {
      console.log(res)
      let resArr = res.data.asset_snap_shoot || []
      let arr1 = []
      let arr2 = []
      resArr.forEach((val, key) => {
        // console.log(val)
        // console.log(key)
        let obj1 = {value: '', date: '', type: '可用数量'}
        let obj2 = {value: '', date: '', type: '冻结数量'}
        let obj0 = {value: '', date: '', type: '总数量'}
        let obj3 = {value: '', date: '', type: '可用数量'}
        let obj4 = {value: '', date: '', type: '冻结数量'}
        let obj5 = {value: '', date: '', type: '总数量'}
        obj1.value = val['base_asset_available']
        obj2.value = val['base_asset_freeze']
        obj0.value = (parseFloat(val['base_asset_available']) + parseFloat(val['base_asset_freeze'])) + ''
        obj3.value = val['quote_asset_available']
        obj4.value = val['quote_asset_freeze']
        obj5.value = (parseFloat(val['quote_asset_available']) + parseFloat(val['quote_asset_freeze'])) + ''
        if (param.type === 'day') {
          obj1.date = new Date(val['create_time'] * 1000).getHours() + ':00'
          obj2.date = new Date(val['create_time'] * 1000).getHours() + ':00'
          obj0.date = new Date(val['create_time'] * 1000).getHours() + ':00'
          obj3.date = new Date(val['create_time'] * 1000).getHours() + ':00'
          obj4.date = new Date(val['create_time'] * 1000).getHours() + ':00'
          obj5.date = new Date(val['create_time'] * 1000).getHours() + ':00'
        } else {
          obj1.date = timestampToTime(val['create_time'], 2)
          obj2.date = timestampToTime(val['create_time'], 2)
          obj0.date = timestampToTime(val['create_time'], 2)
          obj3.date = timestampToTime(val['create_time'], 2)
          obj4.date = timestampToTime(val['create_time'], 2)
          obj5.date = timestampToTime(val['create_time'], 2)
        }
        arr1.push(obj1, obj2, obj0)
        arr2.push(obj3, obj4, obj5)
      })
      _mutations.commit('setSnapShoot1', arr1)
      _mutations.commit('setSnapShoot2', arr2)
      console.log(arr1, arr2)
    })
  }
}

const store = new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})

export default store
